<template>
  <base-layout
    :dialog-title="dialogTitle"
    :dialog-visible="dialogVisible"
    dialog-width="1130px"
    :page-show="pageShow"
    @dialog-close="dialogClose"
  >
    <template #toolPanel />
    <template #searchPanel>
      <search-panel :show-add-button="false" :show-search-button="true"
                    :showExportDataButton="false" labelWidth="73" @search="search" :left="true">
        <el-form-item label="运营商:" prop="operatoruid">
          <el-select v-model="filters.operatoruid" style="width: 212px;" clearable filterable placeholder="请选择运营商">
            <el-option value="0" label="全部">全部</el-option>
            <el-option
              v-for="item in operator"
              :key="item.operatoruid"
              :label="item.operatorname"
              :value="item.operatoruid">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="省份:" prop="provinceuuid">
          <el-select v-model="filters.provinceuuid" style="width: 212px;" clearable filterable  placeholder="请选择省份">
            <el-option value="0" label="全部">全部</el-option>
            <el-option
              v-for="item in info"
              :key="item.provinceuuid"
              :label="item.provincename"
              :value="item.provinceuuid">
              <span style="float: left">{{ item.provincename }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.label }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务类型:" prop="busuuid">
          <el-select v-model="filters.busuuid" clearable filterable placeholder="请选择业务类型">
            <el-option value="0" label="全部">全部</el-option>
            <el-option
              v-for="item in bus"
              :key="item.busuuid"
              :label="item.busname"
              :value="item.busuuid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="供货商:" prop="useruuid">
          <el-select v-model="filters.useruuid" clearable placeholder="请选择供货商"  filterable >
            <el-option value="0" label="全部">全部</el-option>
            <el-option
              v-for="item in usersUp"
              :key="item.user.useruuid"
              :label="item.user.username"
              :value="item.user.useruuid">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="执行状态:" prop="useruuid">
          <el-select v-model="filters.status" clearable placeholder="请选择执行状态" filterable >
            <el-option value="-1" label="全部">全部</el-option>
            <el-option value="0" label="未执行">未执行</el-option>
            <el-option value="1" label="已执行">已执行</el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="开始时间:">
          <el-date-picker type="datetime" placeholder="开始时间"  format="yyyy-MM-dd HH:mm:ss"   v-model="filters.starttime" style="width: 100%;"></el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间:">
          <el-date-picker type="datetime" placeholder="结束时间" clearable format="yyyy-MM-dd HH:mm:ss"   v-model="filters.endtime" style="width: 100%;"></el-date-picker>
        </el-form-item>
      </search-panel>
    </template>
    <template #contentPanel>
      <data-table
        :value="tableData"
        :total-count="totalCount"
        :has-action="true"
        :current="start"
        :width="235"
        :table-column-attributes="tableColumnAttributes"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
        <template #action="{row}">
          <el-button round size="small" @click="detailProductTimer(row)">详情</el-button>
          <el-button round size="small" @click="handleEnable(row)"> {{row.enabled == false ? '禁用' : '启用'}}</el-button>
          <el-button round size="small" @click="executeEnable(row)">执行</el-button>
        </template>
      </data-table>
    </template>

    <template #dialog>
      <el-form :model="matchBatchForm" :inline="true" label-width="100px"   ref="matchBatchForm">
        <!--列表-->
        <el-table :data="upproductsBatch" highlight-current-row max-height="470" border
                    show-overflow-tooltip="true" style="width: 100%">
          <el-table-column type="index" width="70" label="编号">
          </el-table-column>
          <el-table-column prop="upName" width="110px" show-overflow-tooltip label="供货商">
          </el-table-column>
          <el-table-column prop="productId.productCode" show-overflow-tooltip width="120px"   label="产品编码">
          </el-table-column>
          <el-table-column prop="productName" show-overflow-tooltip  width="130px" label="产品名称">
          </el-table-column>
          <el-table-column prop="productId.price" show-overflow-tooltip  width="110px" label="原价">
          </el-table-column>
          <el-table-column prop="beforeDiscount"  width="140px" label="调整前折扣" show-overflow-tooltip>
          </el-table-column>
          <el-table-column prop="beforeValue" label="调整前金额"  width="140px" show-overflow-tooltip  >
          </el-table-column>
          <el-table-column prop="afterDiscount"   width="140px" show-overflow-tooltip label="调整后折扣">
          </el-table-column>
          <el-table-column prop="afterValue"   width="140px" label="调整后金额" show-overflow-tooltip>
          </el-table-column>
        </el-table>
      </el-form>
    </template>

  </base-layout>
</template>

<script>
import baseMixin from '@/mixins/base'
import util from '../../../utils/util'
import {queryProductTimerPageUp,updateProductTimerUp,detailProductTimerUp,executeProductTimerUp} from '@/api/upapi'
import {findAllUpShopInfo,findOpeProBusShop,findAllDownShopInfo} from '@/api/utilsapi'
import moment from 'moment';

export default {
  name: 'producttimerUp',
  mixins: [
    baseMixin({
      idName: 'id',
      findData:queryProductTimerPageUp,
      format(res) {
        return res.map(data => {
          return data
        })
      }
    })
  ],
  data() {
    return {
      pageShow: true,
      dialogData:{},
      users:[],
      usersUp:[],
      tableColumnAttributes: [
        { name: '运营商', value: 'operator' },
        { name: '省份', value: 'province' },
        { name: '业务类型', value: 'busname' },
        { name: '供货名称', value: 'upName' },
        { name: '调价前', value: 'beforeDiscount' },
        { name: '调价后', value: 'afterDiscount'},
        { name: '执行状态', value: 'status' ,formatter:this.productStatus },
        { name: '启用状态', value: 'enabled' ,formatter:this.productEnable },
        { name: '执行时间', value: 'executeTime',width:150 },
        { name: '创建人', value: 'createUser' },
        { name: '创建时间', value: 'createTime' ,width:150},
        { name: '备注', value: 'note' },
      ],
      filters: {
        useruuid:'',
        provinceuuid: '',
        busuuid: '',
        status: '' ,
        operatoruid: '',
        starttime:moment().subtract('hour', 6).format('YYYY-MM-DD HH:mm:ss'),
        endtime:'',
        createUser:''
      },
      userUps:[],
      operator: [],
      downfacevalues:[],
      info: [],
      bus: [],
      matchFormBatchLoading:false,
      matchBatchForm:{},
      upproductsBatch:[]
    }
  },
  methods: {
    async search(form){
      let para = {
        start: 0,
        pageSize: 10,
        upName: this.filters.useruuid == 0 ? "" : this.filters.useruuid,
        province: this.filters.provinceuuid == "0" ? "" : this.filters.provinceuuid,
        busname: this.filters.busuuid == "0" ? "" : this.filters.busuuid,
        status: this.filters.status == "-1" ? "" : this.filters.status  ,
        createUser:'',
        enabled:'',
        operator: this.filters.operatoruid == "0" ? "": this.filters.operatoruid,
        starttime: (this.filters.starttime == "" || this.filters.starttime == undefined) ? "" : moment(this.filters.starttime,"YYYY-MM-DD HH:mm:ss").format('YYYY-MM-DD HH:mm:ss'),
        endtime: (this.filters.endtime == "" || this.filters.endtime == undefined) ? "" : moment(this.filters.endtime,"YYYY-MM-DD HH:mm:ss").format('YYYY-MM-DD HH:mm:ss'),
      };
      //计算供货商名称
      if(this.filters.useruuid){
        let name = this.usersUp.find(item => {return item.user.useruuid == this.filters.useruuid }).user.username;
        para.upName = name;
      }
      if(this.filters.busuuid){
        para.busname = this.bus.find(item =>{ return item.busuuid == this.filters.busuuid; }).busname;
      }
      if(this.filters.operatoruid){
        para.operator = this.operator.find(item =>{ return item.operatoruid == this.filters.operatoruid; }).operatorname;
      }
      if(this.filters.provinceuuid){
        para.province = this.info.find(item =>{ return item.provinceuuid == this.filters.provinceuuid}).provincename;
      }
      this.searchOptions = para;
      this.setTableData()
    },
    async findAllUserShopInfo(){
      let para = {useruuid:''};
      const result = await findAllUpShopInfo(para);
      this.usersUp = result.data;
    },
    //状态转换
    productStatus: function (row, column) {
      return row.status == 0 ? '未执行' : '已执行';
    } ,
    productEnable: function (row, column) {
      return row.enabled == false ? '启用' : '禁用';
    } ,
    handleEnable(row) {
      this.$confirm('确认禁用或者启用此调价吗?', '提示', {
        type: 'warning'
      }).then(() => {
        row.enabled = !row.enabled;
        updateProductTimerUp(row).then(datas => {
          this.resultSuccess(datas);
        });
      }).catch(() => {

      });
    },
    findOpeProBusShop(){
      let para = {sdffds: ''};
      findOpeProBusShop(para).then(datas => {
         this.operator = datas.data.operator;
         this.info = datas.data.info;
         this.bus = datas.data.bus;
      });
    },
    findAllDownShopInfo:function(){
      let para = {useruuid: this.$store.getters.useruuid};
      findAllDownShopInfo(para).then(datas => {
        if(datas.data){
          this.users = datas.data;
        }
      });
    },
    detailProductTimer(row){
      this.dialogTitle = "详情";
      this.dialogVisible = true;
      detailProductTimerUp(row).then(datas => {
         if(datas.data){
           this.upproductsBatch = datas.data;
         }
      });
    },
    executeEnable(row){
      this.$confirm('确认执行此调价吗?', '提示', {
        type: 'warning'
      }).then(() => {
        executeProductTimerUp(row).then(datas => {
          this.resultSuccess(datas);
        });
      }).catch(() => {
      });
    }
  },
  mounted() {
    this.findAllUserShopInfo();
    this.findOpeProBusShop();
    // this.findAllDownShopInfo();
  }
}
</script>
